package com.kouxin.appaccount.software;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class PricingStrategyDao extends HibernateDaoSupport {

	public void insert(PricingStrategy pricingStrategy) {
		this.getHibernateTemplate().save(pricingStrategy);
	}

	@SuppressWarnings("unchecked")
	public List<PricingStrategy> findByExample(PricingStrategy pricingStrategy) {
		return (List<PricingStrategy>) this.getHibernateTemplate().findByExample(pricingStrategy);
	}
	
	public PricingStrategy get(Serializable id) {
		return (PricingStrategy) this.getHibernateTemplate().get(PricingStrategy.class, id);
	}

	public void deleteAll(Collection<PricingStrategy> pricingStrategys) {
		this.getHibernateTemplate().deleteAll(pricingStrategys);
	}

	public void bind(Software software, List<PricingStrategy> pricingStrategys) {
		PricingStrategy pricingStrategy = new PricingStrategy();
		pricingStrategy.setSoftwareId(software.getId());
		deleteAll(findByExample(pricingStrategy));

		if (pricingStrategys != null) {
			for (Iterator<PricingStrategy> iterator = pricingStrategys.iterator(); iterator.hasNext();) {
				pricingStrategy = iterator.next();
				if (pricingStrategy != null) {
					pricingStrategy.setSoftwareId(software.getId());
					insert(pricingStrategy);
				}
			}
		}
	}
}
